﻿using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageHandler.Framework
{
    public class OpenCvHelper
    {
        /// <summary>
        /// 去除干扰线和点
        /// </summary>
        public static void RemoveNoises()
        {
            // 读取验证码图片
            Mat src = Cv2.ImRead("captcha.png", ImreadModes.Grayscale);

            // 二值化处理
            Mat binary = new Mat();
            Cv2.Threshold(src, binary, 128, 255, ThresholdTypes.Binary);

            // 形态学操作去除干扰线
            Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 2));
            Mat morphed = new Mat();
            Cv2.MorphologyEx(binary, morphed, MorphTypes.Open, kernel);

            // 保存处理后的图片
            Cv2.ImWrite("cleaned_captcha.png", morphed);

            Console.WriteLine("干扰线已去除，处理后的图片已保存。");
        }
    }
}
